package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizer;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.stn.zjx.givingtreasure.MainActivity;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.SplashActivity;
import com.stn.zjx.givingtreasure.demo.RobotMsgDomain;
import com.stn.zjx.givingtreasure.utils.StatusBarUtil;
import com.vondear.rxtools.RxActivityTool;
import com.vondear.rxtools.RxNetTool;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class RobotMsgItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
     public static final int Robot_Header=1001;
    public List<RobotMsgDomain> dataBeanList = new ArrayList<>();
    private LayoutInflater inflater;
    public Context context;
    public SpeechSynthesizer speechSynthesizer;

    public RobotMsgItemListAdapter(SpeechSynthesizer speechSynthesizer,Context context) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.speechSynthesizer=speechSynthesizer;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh = null;
        switch (viewType) {
            case 0:
                vh = new RobotMsgTextIndexHolder(inflater.inflate(R.layout.item_robot_msg_text_left, parent,
                        false));

                break;
            case 1:
                vh = new UserMsgTextIndexHolder(inflater.inflate(R.layout.item_robot_msg_text_right, parent, false));

                break;

            case Robot_Header:
                 vh=new HeaderIndexHolder(inflater.inflate(R.layout.item_robot_header, parent, false));

                break;
        }
        return vh;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        RobotMsgDomain data = dataBeanList.get(i);
        HeaderIndexHolder hih;
        RobotMsgTextIndexHolder rmtIh;
        UserMsgTextIndexHolder umtIh;
        switch (getItemViewType(i)) {
            case 0:
                rmtIh = (RobotMsgTextIndexHolder) viewHolder;
                rmtIh.loading.setVisibility(View.VISIBLE);
                rmtIh.tv.setText("");
                rmtIh.tv.setVisibility(View.GONE);
                robotSpeak(rmtIh.tv,rmtIh.loading,data);
                break;
            case 1:
                umtIh = (UserMsgTextIndexHolder) viewHolder;
                umtIh.tv.setText(data.message);
                break;
            case Robot_Header:
                hih= (HeaderIndexHolder) viewHolder;
                break;
        }
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    public void refreshList(List<RobotMsgDomain> dataBeans) {
        dataBeanList.removeAll(dataBeanList);
        dataBeanList.addAll(dataBeans);
        notifyDataSetChanged();
    }
    public void append(RobotMsgDomain dataBeans) {
        dataBeanList.add(dataBeans);
//        notifyDataSetChanged();
        notifyItemInserted(this.dataBeanList.size()-1);
    }
    public void appendList(List<RobotMsgDomain> dataBeans) {
        dataBeanList.addAll(dataBeans);
        notifyDataSetChanged();
    }

    public void clearList() {
        dataBeanList.removeAll(this.dataBeanList);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        RobotMsgDomain domain = dataBeanList.get(position);
        return domain.message_user_type;
    }
    public synchronized void robotSpeak(TextView tv,LinearLayout loading,RobotMsgDomain domain){
        if(domain.isConnectSucess==0){

            if(!RxNetTool.isNetworkAvailable(context)){
                loading.setVisibility(View.GONE);
                tv.setVisibility(View.VISIBLE);
                tv.setText(context.getString(R.string.internet_conn_error));
                domain.isConnectSucess=1;
                return;
            }

            Disposable disposable=Observable.create(new ObservableOnSubscribe<Integer>() {
                @Override
                public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                    Thread.sleep(1000);
                    emitter.onNext(11);
                    emitter.onComplete();
                }
            })
                    .doOnSubscribe(new Consumer<Disposable>() {
                        @Override
                        public void accept(Disposable disposable) throws Exception {
                            loading.setVisibility(View.VISIBLE);
                            tv.setText("");
                            tv.setVisibility(View.GONE);
                        }
                    })
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnComplete(new Action() {
                        @Override
                        public void run() throws Exception {

                        }
                    })
                    .subscribe(new Consumer<Integer>() {
                        @Override
                        public void accept(Integer integer) throws Exception {
                            System.out.println("*******************"+domain.message);
                            loading.setVisibility(View.GONE);
                            tv.setVisibility(View.VISIBLE);
                            tv.setText(domain.message);
                            speechSynthesizer.speak(""+domain.message);
                            domain.isConnectSucess=2;
                        }
                    });

        }else if(domain.isConnectSucess==1){
            loading.setVisibility(View.GONE);
            tv.setVisibility(View.VISIBLE);
            tv.setText(context.getString(R.string.internet_conn_error));
        }else if(domain.isConnectSucess==2){
            loading.setVisibility(View.GONE);
            tv.setVisibility(View.VISIBLE);
            tv.setText(domain.message);
        }

    }
    public class RobotMsgTextIndexHolder extends RecyclerView.ViewHolder {

        public View rootView;
        @BindView(R.id.tv)
        TextView tv;
        @BindView(R.id.loading)
        LinearLayout loading;
        RobotMsgTextIndexHolder(@NonNull View itemView) {
            super(itemView);
            rootView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }

    public class UserMsgTextIndexHolder extends RecyclerView.ViewHolder {

        public View rootView;
        @BindView(R.id.tv)
        TextView tv;
        UserMsgTextIndexHolder(@NonNull View itemView) {
            super(itemView);
            rootView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }
    public class HeaderIndexHolder extends RecyclerView.ViewHolder {

        public View rootView;

        HeaderIndexHolder(@NonNull View itemView) {
            super(itemView);
            rootView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }


    public synchronized void sendMessage(final int userType, String message){
        if(userType==0){
             RobotMsgDomain robotMsgDomain=new RobotMsgDomain();
             robotMsgDomain.message=message;
             robotMsgDomain.message_user_type=0;
             robotMsgDomain.message_type=0;
              append(robotMsgDomain);
        }else{
            speechSynthesizer.stop();
            RobotMsgDomain robotMsgDomain=new RobotMsgDomain();
            robotMsgDomain.message=message;
            robotMsgDomain.message_user_type=1;
            robotMsgDomain.message_type=0;
            append(robotMsgDomain);


            RobotMsgDomain data=new RobotMsgDomain();
            data.message="小送没有听懂主人说什么，请再说一次吧~";
            data.message_user_type=0;
            data.message_type=0;
            append(data);

        }
    }
}